package msu.ml.core.xml;

import msu.ml.core.*;
import msu.ml.data.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;

/**
 * @author Reginald M Mead
 * @version 1.0
 */
public class ManualValidationDataProviderParser implements IDataProviderParser
{
   /**
    *
   */
	public IDataProvider parse(Element element)
	{
        boolean clearCache = false;
        if(element.hasAttribute("clear_cache"))
            clearCache = Boolean.parseBoolean(element.getAttribute("clear_cache"));
      DbManualValidationDataProvider provider = 
         new DbManualValidationDataProvider(clearCache);

      String directory = element.getAttribute("directory");
      provider.setDirectoryPath(directory);

      NodeList nodes = element.getElementsByTagName("validation_set");
      for(int i = 0; i < nodes.getLength(); i++)
      {
         Element elem = ((Element)nodes.item(i));


         provider.addValidationSet(
            getFiles((Element)(elem.getElementsByTagName(
               "training_files").item(0))),
            getFiles((Element)(elem.getElementsByTagName(
               "target_files").item(0))));
      }

		if(ApplicationSettings.getInstance().getConfig("verbose").equals("true"))
		{
			System.out.println("Manual Validation Data Provider");
			System.out.println("\t Contains " + 
            provider.getNumValidationSets() + " validation sets");
		}

      return provider;
	}

   /**
   * Get all of the file elements
   */
   private String [] getFiles(Element element)
   {
      NodeList nodes = element.getElementsByTagName("file");
      String [] result = new String[nodes.getLength()];
      for(int i = 0; i < nodes.getLength(); i++)
         result[i] = nodes.item(i).getTextContent();
      return result;
   }
}
